// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "./gameData";
import sceneMediator from "../framework/sceneMediator";
import mainManage from "../framework/mainManage";
// import sceneMediator from "../resources/framework/sceneMediator";
// import mainManage from "../resources/framework/mainManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class savePetMediator extends cc.Component {

    @property()
    specialScene : boolean = false;

    index = -1;


    onLoad(){
        if(!this.specialScene){
            let data = gameData.getPetShopData();
            for(let i in data){
                let stage =  data[i]["getstage"];
                if(typeof stage == "number"){
                    if(sceneMediator.getShowScene() == stage){
                        this.index = parseInt(i);
                    }
                }
            }
            if(this.index == -1){
                this.node.active = false;
            }else{
                if(gameData.getPetProcessSkin()[this.index] == 0){
                    this.node.active = true;
                }else{
                    this.node.active = false;
                }
            }
        }else{
            this.index = 15;
        }
    }

    onBeginContact(contact, self, other) {
        if(other.node.group == "role"){
            cc.game.emit("shieldPressKey",true);

            this.scheduleOnce(()=>{
                let spk = this.node.getChildByName("spine").getComponent(sp.Skeleton);
                spk.setAnimation(0,"daiji",true);
                mainManage.createToast("Selamat! Anda telah mendapatkan seekor hewan peliharaan");
                gameData.savePetSkin(this.index);
                let arr = gameData.getPetCurSkin();
                if(arr.length >= gameData.judgeWearPetNumber(gameData.curRankTitle)){
                // if(arr.length >= 2){
                    cc.game.emit("showMorePet",this.index);
                    this.scheduleOnce(()=>{
                        cc.game.emit("shieldPressKey",false);
                        this.node.destroy();
                    },1)
                }else{
                    arr.push(this.index);
                    gameData.setPetCurSkin(arr);
                    gameData.saveLastPetSkin();
                    cc.game.emit("resetPet");
                    this.scheduleOnce(()=>{
                        this.node.destroy();
                        cc.game.emit("shieldPressKey",false);
                    },1)
                }
            },2)
        }
    }
}
